home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Swar / init.c < prev    next >
Text File  |  1994-08-21  |  4KB  |  205 lines

  1.  
  2. #include "swar.h"
  3.  
  4. #define NIL_POINTER        0L
  5.  
  6. ToolBoxInit()
  7. {
  8.     InitGraf(&thePort);
  9.     InitFonts();
  10.     FlushEvents(everyEvent, 0);
  11.     InitWindows();
  12.     InitMenus();
  13.     TEInit();
  14.     InitDialogs(0L);
  15.     InitCursor();
  16.     MenuBarInit();
  17. }
  18.  
  19. InitColors()
  20. {
  21.     extern RGBColor    myGreen, myRed, myBlue, myYellow, myWhite, myBlack, myGray, myOrange, myDkBlue;
  22.     
  23.     myGreen.red = 0;
  24.     myGreen.green = -1;
  25.     myGreen.blue = 0;
  26.     myYellow.red = -1;
  27.     myYellow.green = -1;
  28.     myYellow.blue = 0;
  29.     myRed.red = -1;
  30.     myRed.green = 0;
  31.     myRed.blue = 0;
  32.     myBlue.red = 25000;
  33.     myBlue.green = 55000;
  34.     myBlue.blue = -1;
  35.     myDkBlue.red = 10000;
  36.     myDkBlue.green = 10000;
  37.     myDkBlue.blue = -1;
  38.     myWhite.red = -1;
  39.     myWhite.green = -1;
  40.     myWhite.blue = -1;
  41.     myBlack.red = 0;
  42.     myBlack.green = 0;
  43.     myBlack.blue = 0;
  44.     myGray.red = 20000;
  45.     myGray.green = 20000;
  46.     myGray.blue = 20000;
  47.     myOrange.red = 40000;
  48.     myOrange.green = 25000;
  49.     myOrange.blue = 8000;
  50. }
  51.  
  52. MenuBarInit()
  53. {
  54.     Handle    myMenuBar;
  55.     extern MenuHandle    gAppleMenu, gFileMenu, gEditMenu;
  56.     
  57.     myMenuBar = GetNewMBar(500);
  58.     SetMenuBar(myMenuBar);
  59.     gFileMenu = GetMHandle(501);
  60.     gEditMenu = GetMHandle(502);
  61.     gAppleMenu = GetMHandle(500);
  62.     if (gAppleMenu)
  63.         AddResMenu(gAppleMenu, 'DRVR');
  64.     
  65. } /* MenuBarInit() */
  66.  
  67. LoadSounds()
  68. {
  69.     extern Handle            gNewMatchSoundH;
  70.     extern Handle            gShotSoundH;
  71.     extern Handle            gBoomSoundH;
  72.     extern Handle            gEndorsementSndH;
  73.  
  74.     LoadSound(&gNewMatchSoundH, 501, "\p'snd ' New Level (501)");
  75.     LoadSound(&gShotSoundH, 502, "\p'snd ' Shot (502)");
  76.     LoadSound(&gBoomSoundH, 503, "\p'snd ' Boom (503)");
  77.     AInitSnd();
  78.  
  79. } /* LoadSounds() */
  80.  
  81. UnloadSounds()
  82. {
  83.     extern Handle            gNewMatchSoundH;
  84.     extern Handle            gShotSoundH;
  85.     extern Handle            gBoomSoundH;
  86.     extern Handle            gEndorsementSndH;
  87.  
  88.     UnloadSound(gNewMatchSoundH);
  89.     UnloadSound(gShotSoundH);
  90.     UnloadSound(gBoomSoundH);
  91.     AStopSnd();
  92.  
  93. } /* UnloadSounds() */
  94.  
  95. CGrafPort *
  96. PortInit(myRect, depth, portName)
  97.     Rect    myRect;
  98.     short    depth;
  99.     Str255    portName;
  100. {
  101.     long        bytes;
  102.     Ptr            myBits;
  103.     GrafPtr        savePort;
  104.     GrafPtr        newGrafPtr;
  105.     CGrafPtr    newCGrafPtr;
  106.     
  107.     GetPort(&savePort);
  108.     if ((newCGrafPtr = (CGrafPtr)NewPtr(sizeof(CGrafPort))) == NIL_POINTER) {
  109.         ExitAppl();
  110.     } /* if */
  111.     OpenCPort(newCGrafPtr);
  112.     bytes = (((depth * (myRect.right - myRect.left)) + 15) >> 4) << 1;
  113.     myBits = NewPtr(bytes * (myRect.bottom - myRect.top));
  114.     if (depth == 1) {
  115.         newGrafPtr = (GrafPtr) newCGrafPtr;
  116.         (newGrafPtr->portBits).baseAddr = myBits;
  117.         (newGrafPtr->portBits).rowBytes = bytes;
  118.         (newGrafPtr->portBits).bounds = myRect;
  119.     } /* if */
  120.     else {
  121.         (**(*newCGrafPtr).portPixMap).baseAddr = myBits;
  122.         (**(*newCGrafPtr).portPixMap).rowBytes = bytes | 0x8000;
  123.         (**(*newCGrafPtr).portPixMap).bounds = myRect;
  124.         (**(*newCGrafPtr).portPixMap).hRes = 72;    
  125.         (**(*newCGrafPtr).portPixMap).vRes = 72;
  126.         (*newCGrafPtr).portRect = myRect;
  127.     } /* else */
  128.  
  129.     //        Must set FG and BG pens for black background
  130.     //    
  131.     RGBForeColor(&myBlack);
  132.     RGBBackColor(&myBlack);
  133.  
  134.     //        Erase entire port
  135.     //    
  136.     EraseRect(&myRect);
  137.  
  138.     //        restore old port value (game screen)
  139.     //
  140.     SetPort(savePort);
  141.  
  142.     return(newCGrafPtr);
  143.  
  144. } /* PortInit() */
  145.  
  146. WindowInit()
  147. {
  148.     Rect         myRect;
  149.     short        depth;
  150.     GrafPtr        savePort;
  151.     extern        CWindowPtr    gPictureWindow;
  152.     extern        CGrafPort        *gOSPtr, *gScrapPtr;
  153.  
  154.     GetPort(&savePort);
  155.     gPictureWindow = (CWindowPtr)GetNewCWindow(500, NIL_POINTER, (WindowPtr) -1L);
  156.     SetPort((GrafPtr)gPictureWindow);
  157.     
  158.     //        Must set FG and BG pens for CopyBits to work
  159.     //    
  160.     RGBForeColor(&myBlack);
  161.     RGBBackColor(&myWhite);
  162.  
  163.     //        Create offscreen port for game playing field
  164.     //
  165.     myRect = gPictureWindow->portRect;
  166.     depth = (**(*gPictureWindow).portPixMap).pixelSize;
  167.     gOSPtr = (CGrafPort *)gPictureWindow;
  168.     
  169.     //        Create scrap port containing game pieces
  170.     //
  171.     myRect.top = 0;
  172.     myRect.bottom = 329;
  173.     myRect.left = 0;
  174.     myRect.right = 329;
  175.     if ((gScrapPtr = PortInit(myRect, depth, "\pScrap Port")) == NIL_POINTER) {
  176.         ExitAppl();
  177.     } /* if */
  178.     
  179.     SetPort(savePort);
  180.         
  181. } /* WindowInit() */
  182.  
  183. LoadPieces()
  184. {
  185.     short         i, h, s;
  186.     Rect        scrapPortRect;
  187.     GrafPtr        savePort;
  188.     PicHandle    gamePiecePicH;
  189.  
  190.     GetPort(&savePort);
  191.     SetPort((GrafPtr)gScrapPtr);
  192.     
  193.     //        Load game pieces, then draw them into the scrap port
  194.     //
  195.     if ((gamePiecePicH = GetPicture(500)) == 0L) {
  196.         ExitAppl();
  197.     } /* if */
  198.     scrapPortRect = (**(gamePiecePicH)).picFrame;
  199.     DrawPicture(gamePiecePicH, &scrapPortRect);
  200.     ReleaseResource((Handle)gamePiecePicH);
  201.  
  202.     SetPort(savePort);
  203.  
  204. } /* LoadPieces() */
  205.